package com.mangosteen.interviewji.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mangosteen.interviewji.common.BaseResponse;
import com.mangosteen.interviewji.common.ErrorCode;
import com.mangosteen.interviewji.common.ResultUtils;
import com.mangosteen.interviewji.exception.BusinessException;
import com.mangosteen.interviewji.exception.ThrowUtils;
import com.mangosteen.interviewji.mapper.UserQuestionMapper;
import com.mangosteen.interviewji.model.dto.UserQuestion.UserQuestionRequest;
import com.mangosteen.interviewji.model.entity.User;
import com.mangosteen.interviewji.model.entity.UserQuestion;
import com.mangosteen.interviewji.model.vo.QuestionVO;
import com.mangosteen.interviewji.service.UserQuestionService;
import com.mangosteen.interviewji.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/userQuestion")
public class UserQuestionController {

    @Resource
    private UserQuestionService userQuestionService;

    @Resource
    private UserService userService;

    @Resource
    private UserQuestionMapper userQuestionMapper;
    @PostMapping("/submit")
    public BaseResponse<UserQuestion> submit(@RequestBody UserQuestionRequest userQuestionRequest, HttpServletRequest  request) {
        String userAnswer=userQuestionRequest.getUserAnswer();
        Long questionId=userQuestionRequest.getQuestionId();
        log.info("提交用户答案：{}", userAnswer);
        if (userAnswer == null){
            throw new RuntimeException("用户答案不能为空");
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        UserQuestion userQuestion = userQuestionService.submit(userAnswer,questionId,loginUser);
        return ResultUtils.success(userQuestion);
    }

    @PostMapping("/getUserQuestion")
    public BaseResponse<UserQuestion> getUserQuestion(@RequestParam Long questionId, HttpServletRequest  request) {
        log.info("获取用户答案：{}", questionId);
        if (questionId==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        User loginUser = userService.getLoginUser(request);
        QueryWrapper<UserQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id",questionId);
        queryWrapper.eq("user_id",loginUser.getId());
        UserQuestion userQuestion = userQuestionMapper.selectOne(queryWrapper);
        return ResultUtils.success(userQuestion);
    }
    @GetMapping("/get/UserRecordHistory")
    public BaseResponse<Page<QuestionVO>> getUserRecord(@RequestParam Long current, @RequestParam Long size, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);

        return ResultUtils.success(userQuestionService.getUserRecordHistory(current,size,loginUser.getId(), request));
    }
}
